/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Manish Mehta 
 												
Class							:				NoBuildingExistsBOFix
 
Function						: 				A class for holding fixes for the case 
												when a basic operator fails because of 
												unavailability of a building
****************************************************************************/
package behavioradaptation.fixes;


import planninglanguage.plans.BuildBOPlan;
import planninglanguage.plans.MultiPlan;
import planninglanguage.plans.Plan;
import planninglanguage.plans.SequentialPlan;
import base.WargusGameState;
import base.WargusStateImporter;
import behavioradaptation.reasoner.GameStateHelpers;

public class NoBuildingExistsBOFix {
	public static Plan fix(Plan a_failedPlan,WargusGameState a_gameState, String a_buildingType) throws Exception
	{
		int ID = -1;
		int i = 0;
		String []bldgs = {"town-hall","keep","castle","great-hall","stronghold","fortress"};
		
		for(i=0,ID=-1;i<6 && ID==-1;i++) {
			if(GameStateHelpers.GetUnitIDofType(a_gameState,0,bldgs[i]).size() != 0) {
				ID = GameStateHelpers.GetUnitIDofType(a_gameState,0,bldgs[i]).get(0);
			}
		}
		if (ID == -1) return null;
		// Find the closest match of x and y coordinates
		int x = 0, y = 0; 
		MultiPlan fixedPlan = new SequentialPlan();
		// add a build plan for building the building 
		fixedPlan.add( new BuildBOPlan(ID,x, y, WargusStateImporter.unitTypeToInteger(a_buildingType),null,null,null) );
		fixedPlan.add((Plan)a_failedPlan.clone());
		
		return fixedPlan;
	}
	
}
